package openGLTest.code;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.R.string;
import android.content.Context;

import android.graphics.*;
import android.text.StaticLayout;

public class GLCube {
	
	private static final byte[] vertices = {

		-1,-1,-1, 	//V0
		-1,-1,1,	//v1
		1,-1,1,		//v2
		1,-1,-1,	//v3
		-1,1,-1,	//v4
		-1,1,1,		// v5
		1,1,1,		// v6
		1,1,-1		//v7
		
//		-1,-1,-1, 	//V0
//		1,-1,-1,	//v1
//		1,1,-1,		//v2
//		-1,1,-1,	//v3
//		-1,-1,1,	//v4
//		1,-1,1,		// v5
//		1,1,1,		// v6
//		-1,1,1		//v7
		};
	
	private static final byte[] colors = {

		127,0,0,1, 	// v0	
		0,127,0,1,	//v1
		0,0,127,1,	// v2	
		127,0,0,1,	//v3
		0,127,0,1,
		0,0,127,1,
		127,0,0,1,
		0,127,0,1,
		

//		127,0,0,1,
//		127,0,0,1,
//		127,0,0,1,
//		127,0,0,1,
//		0,127,0,1,
//		0,127,0,1,
//		0,127,0,1,
//		0,127,0,1,
		
	};
	
	private static final byte[] normals = {
		0,0,127,  0,0,127,  
		0,0,127,   0,0,127,
		0,0,-128, 0,0,-128,  0,0,-128,  0,0,-128,
		0,-128,0, 0,-128,0,   0,-128,0,  0,-128,0,
		0,127,0,  0,127,0,    0,127,0,   0,127,0,
		127,0,0,  127,0,0,    127,0,0,   127,0,0,
		-128,0,0, -128,0,0,   -128,0,0,  -128,0,0
		};
	

	
	private static final byte[] texturecoordinates = {
		0,1,1,1,   0,0,1,0,
		1,1,1,0,   0,1,0,0,
		1,1,1,0,   0,1,0,0,
		1,1,1,0,   0,1,0,0,
		1,0,0,0,   1,1,0,1,
		0,0,0,1,   1,0,1,1};
	
	private static final byte[] indices = {

		0,1,2,
		0,2,3,
		0,3,4,
		3,7,4,
		0,4,5,
		0,5,1,
		6,7,2,
		6,4,7,
		6,5,4,
		6,1,5,
		6,2,1,
		3,2,7
	};
	
	
	
	private ByteBuffer cubeVertices;
	private ByteBuffer  cubeColors;
	private ByteBuffer cubeNormals;
	private ByteBuffer cubeIndices;
	private ByteBuffer cubeTexture;
	
	public GLCube(){
		cubeVertices = ByteBuffer.allocateDirect(vertices.length);
		cubeVertices.put(vertices);
		cubeVertices.rewind();
		
		cubeColors = ByteBuffer.allocateDirect(colors.length);
		cubeColors.put(colors);
		cubeColors.rewind();
		
		cubeNormals = ByteBuffer.allocateDirect(normals.length);
		cubeNormals.put(normals);
		cubeNormals.rewind();		
		
		cubeIndices = ByteBuffer.allocateDirect(indices.length);
		cubeIndices.put(indices);
		cubeIndices.rewind();
		
		cubeTexture = ByteBuffer.allocateDirect(texturecoordinates.length);
		cubeTexture.put(texturecoordinates);
		cubeTexture.rewind();
		
	}
	
	public void draw(GL10 gl10) {
		
		gl10.glVertexPointer(3, GL10.GL_BYTE, 0, cubeVertices);
		gl10.glColorPointer(4, GL10.GL_UNSIGNED_BYTE, 0, cubeColors);
		gl10.glNormalPointer(GL10.GL_BYTE, 0, cubeNormals);
		
		gl10.glTexCoordPointer(2, GL10.GL_BYTE, 0, cubeTexture);
		
		//am schluss!!
		gl10.glDrawElements(GL10.GL_TRIANGLES, 36, GL10.GL_UNSIGNED_BYTE, cubeIndices);
		gl10.glFinish();
		
	}
	
	public void loadTexture(GL10 gl10,Context context, int res) {
	
		Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), res);
		ByteBuffer myByteBuffer = convertRGBA(bitmap);
		loadTextureBitmap(gl10,myByteBuffer,bitmap.getWidth(),bitmap.getHeight());
		
	}
	
	private static ByteBuffer convertRGBA(Bitmap bitmap){
		
		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bitmap.getHeight()*bitmap.getWidth()*4);
		byteBuffer.order(ByteOrder.BIG_ENDIAN);
		IntBuffer myIntBuffer = byteBuffer.asIntBuffer();
		for (int y = 0; y < bitmap.getHeight(); y++) {
			for (int x = 0; x < bitmap.getWidth(); x++) {
				int pixel = bitmap.getPixel(x, bitmap.getHeight()-y -1);
				int red = ((pixel >> 16)& 0xFF);
				int green = ((pixel >> 8)& 0xFF);
				int blue = ((pixel)& 0xFF);
				
				myIntBuffer.put(red << 24 | green << 16 | blue << 8 | (red + blue + green) / 3);
				
			}
			
		}
		myIntBuffer.rewind();
		return byteBuffer;
		
	}
	
	private static void loadTextureBitmap(GL10 gl10, ByteBuffer byteBuffer, int width, int height) {
		
		int[] textureName = new int[1];
		gl10.glGenTextures(1, textureName, 0);
		int texture = textureName[0];
		gl10.glBindTexture(GL10.GL_TEXTURE_2D, texture);
		
		gl10.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, width, height, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, byteBuffer);
		
		gl10.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
		
		gl10.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

		
	}
}
/*
private static final byte[] vertices = {
	-1,1,1,  1,-1,1, 
	1,1,1,  -1,-1,1,
	-1,1,-1, 1,-1,-1, 1,1,-1, -1,-1,-1,
	-1,-1,1, 1,-1,-1, 1,-1,1, -1,-1,-1,
	-1,1,1,  1,1-1,   1,1,1,   -1,1,-1,
	 1,-1,1, 1,1,-1,  1,1,1,   1,-1,-1,
	-1,-1,1,-1,1,-1, -1,1,1,  -1,-1,-1
	};

private static final byte[] colors = {
	127,0,0,  127,127,0, 0,127
	,127,
	0,0,127,  127,0,0,   127,0,127,
	0,127,0,  127,0,127, 0,127,0,
	127,0,127, 0,127,0,  0,127,127,
	0,0,127,   127,0,0,   127,127,0,
	0,127,127, 127,0,127, 127,127,0,
	127,127,127, 0,127,127,  127,0,127,
	127,127,127, 0,127,127,  127,0,127,
	127,127,0,   127,127,127, 0,127,0,
	127,127,127, 0,127,127,   127,0,127,
	127,127,0,   127,127,127, 
	
	0,127,127
	
};

private static final byte[] normals = {
	0,0,127,  0,0,127,  
	0,0,127,   0,0,127,
	0,0,-128, 0,0,-128,  0,0,-128,  0,0,-128,
	0,-128,0, 0,-128,0,   0,-128,0,  0,-128,0,
	0,127,0,  0,127,0,    0,127,0,   0,127,0,
	127,0,0,  127,0,0,    127,0,0,   127,0,0,
	-128,0,0, -128,0,0,   -128,0,0,  -128,0,0
	};



private static final byte[] texturecoordinates = {
	0,1,1,1,   0,0,1,0,
	1,1,1,0,   0,1,0,0,
	1,1,1,0,   0,1,0,0,
	1,1,1,0,   0,1,0,0,
	1,0,0,0,   1,1,0,1,
	0,0,0,1,   1,0,1,1};

private static final byte[] indices = {
0,3,1,   2,0,1,
6,5,4,   5,7,4,
8,11,9,  10,8,9,
15,12,13,  12,14,13,
16,19,17,  18,16,17,
23,20,21,   20,22,21
};
*/